package com.youle.contoller;

import com.youle.enums.ExceptionEnum;
import com.youle.exception.YLException;
import com.youle.item.pojo.ItemCategory;
import com.youle.service.ItemCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by
 *  查询商品分类的controller    http://api.leyou.com/api/item/category/list?pid=0
 * @author touni
 * @date 2019/4/17
 * @time 18:58
 */
@RestController
@RequestMapping("category")
public class ItemCategoryController {


    @Autowired
    private ItemCategoryService itemCategoryService;

    @GetMapping("list")
    public ResponseEntity<List<ItemCategory>> listItemCategoryByParentId(@RequestParam(defaultValue = "0") Long pid,
                                                                        HttpServletResponse response){
        //解决AJAX跨域的问题  不推荐这种方式 已经在zuul中进行了配置
        //response.setHeader("Access-Control-Allow-Origin", "*");
        /*  测试异常处理的
        if (pid == 0){
            throw new YLException(ExceptionEnum.PRICE_NOT_BE_NULL);
        }*/
        return itemCategoryService.listItemCategoryByParentId(pid);
    }


    /**
     * http://api.leyou.com/api/item/category/add?name=%E6%96%B0%E7%9A%84%E8%8A%82%E7%82%B9&parentId=103&sort=7
     * 增加category 前端页面没有完成修改后的回写功能    todo 此处前端没有进行回显，导致在新创建的类目下在不刷新的情形下再次添加会抛出空指针
     * @param name
     * @param parentId
     * @param sort
     * @return
     */
    @GetMapping("add")
    public ResponseEntity<ItemCategory> addItemCategoryByParentId(String name,Long parentId,Integer sort){
        if (null == name || null == parentId || null == sort){
            throw new YLException(ExceptionEnum.PARAM_NOT_BE_NULL);
        }
        return itemCategoryService.addItemCategoryByParenId(name,parentId,sort);
    }


    /**
     * http://api.leyou.com/api/item/category/delete?id=1427
     *
     * 用来删除节点的
     *
     * @param id
     * @return
     */
    @GetMapping("delete")
    public ResponseEntity deleteItemCategoryById(Long id){
        if (null == id){
            throw new YLException(ExceptionEnum.PARAM_NOT_BE_NULL);
        }
        return itemCategoryService.deleteItemCategoryById(id);
    }


    /**
     * 参数如下
     * id: node.id,
     * name: node.name,
     *
     * 根据id来修改类目的数据   TODO 此处Vue前端无法触发AJAX请求 待解决
     *
     * @return
     */
    @GetMapping("edit")
    public ResponseEntity editItemCategory(Long id,String name){
        if (null == id || null == name){
            throw new YLException(ExceptionEnum.PARAM_NOT_BE_NULL);
        }
        return itemCategoryService.editItemCategory(id,name);

    }















}
